﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace OgilvyOne.Utility
{
    /// <summary>
    /// 分页类
    /// </summary>
    public class PageTools
    {
        private DataSet dataset;
        private IList datalist;
        private int pageno;
        private int pagesize;
        private int counts;
        private bool list = false;
        
        /// <summary>
        /// 分页实例
        /// </summary>
        /// <param name="dataset">数据</param>
        /// <param name="pageno">当前页</param>
        /// <param name="pagesize">每页数</param>
        /// <param name="counts">总记录数</param>
        public PageTools(DataSet dataset, int pageno, int pagesize, int counts)
        {
            this.dataset = dataset;
            this.pageno = pageno;
            this.pagesize = pagesize;
            this.counts = counts;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="datalist">当前数据</param>
        /// <param name="pageno"></param>
        /// <param name="pagesize"></param>
        /// <param name="counts"></param>
        public PageTools(IList datalist, int pageno, int pagesize, int counts)
        {
            this.datalist = datalist;
            this.pageno = pageno;
            this.pagesize = pagesize;
            this.counts = counts;
            list = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="datalist">所有数据</param>
        /// <param name="pageno"></param>
        /// <param name="pagesize"></param>
        public PageTools(IList datalist, int pageno, int pagesize)
        {
            this.datalist = new ArrayList();
            for (int i = (pageno - 1) * pagesize; i < pageno * pagesize&&i<datalist.Count; i++)
            {
                this.datalist.Add(datalist[i]);
            }
            this.pageno = pageno;
            this.pagesize = pagesize;
            this.counts = datalist.Count;
            list = true;
        }

        public IList DataList
        {
            get { return datalist; }
            set { datalist = value; }
        }

        public bool IsDataSet
        {
            get { return list == false; }
        }

        public DataSet DataSet
        {
            get { return dataset; }
            set { dataset = value; }
        }
        public int PageNo
        {
            get { return pageno; }
            set { pageno = value; }
        }
        public int PageSize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }
        public int Counts
        {
            get { return counts; }
            set { counts = value; }
        }
        //总页数
        public int PageCount()
        {
            return counts / pagesize + (counts % pagesize > 0 ? 1 : 0);
        }
        //下一页
        public int NextPage()
        {
            int pc = PageCount();
            return pageno < pc ? pageno + 1 : pc;
        }
        //上一页
        public int PrevPage()
        {
            return pageno > 1 ? pageno - 1 : 1;
        }
    }
}
